//
//  SettingViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/7/1.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "SettingViewController.h"
#import "NetSettingViewController.h"
#import <MessageUI/MessageUI.h>

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,MFMessageComposeViewControllerDelegate>
{
    NSMutableArray *_arr2;
    NSMutableArray *_arr1;
 }

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showUI];
    [self initData];
    
}

-(void)initData{
    _dataArr = [[NSMutableArray alloc] init];
    _arr1 = [[NSMutableArray alloc]initWithObjects:@"清除缓存",@"网络设置",@"提示音", nil];
    _arr2 =  [[NSMutableArray alloc]initWithObjects:@"关于我们",@"喜欢就分享给好友",@"版本V1.0",@"感谢收听", nil];
    [_dataArr addObject:_arr1];
    [_dataArr addObject:_arr2];
    
    
    
    
    
    

}
-(void)showUI{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenSize.width , kScreenSize.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    
    
    
    [self.view addSubview:self.tableView];
    
    
    [self.tableView reloadData];
    
    
    
}

#pragma mark - 获取缓存大小
-(double)getCachesSize{
    //换算为m
    double sdSize = [[SDImageCache sharedImageCache]getSize];
    //获取自定义的缓存
    NSString *myCachePath = [NSHomeDirectory()stringByAppendingFormat:@"/Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator =  [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    double mySize = 0;
    for (NSString *fileName in enumerator) {
        NSString *filePath = [myCachePath stringByAppendingString:fileName];
        NSDictionary *dict=  [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
        mySize += dict.fileSize;
        
    }
    double totalSize = (mySize+sdSize)/1024/1024;
    return totalSize;
    
}





#pragma mark UITableView


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArr[section]count];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    if (!cell) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        
        
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(kScreenSize.width-60, 15, 100, 30)];
            [sw addTarget:self action:@selector(swClick:) forControlEvents:UIControlEventValueChanged];
            sw.tag = indexPath.row+101;
            [cell.contentView addSubview:sw];
            
        }
    }
    cell.textLabel.text = _dataArr[indexPath.section][indexPath.row];
    
    
    return cell;
    
    
}

-(void)swClick:(UISwitch *)sw{

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //清楚缓存
            NSString *title = [NSString stringWithFormat:@"删除缓存文件:%.6fM",[self getCachesSize]];
            UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:nil];
            [sheet showInView:self.view];

            
        }else if (indexPath.row == 1){
            NetSettingViewController *net = [[NetSettingViewController alloc] init];
            [self.navigationController pushViewController:net animated:YES];
        
        }
    }
    else if(indexPath.section == 1){
        switch (indexPath.row) {
            case 0:
            {
               //@"关于我们"
            }
                break;
            case 1:
            {
                //@"官方微博
                 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:10086"]];
                
            }
                break;
            case 2:
            {
                //版本V1.0
               
                
                
                
            }
                break;
            case 3:
            {
               // @"感谢收听
                
                
            }
                break;
                
            default:
                break;
        }
    
      }
    }
    
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == actionSheet.destructiveButtonIndex) {
        //删除(两部分 sdimage  一个是我们自己的)
        [[SDImageCache sharedImageCache] clearMemory];
        //清楚磁盘上的图片
        [[SDImageCache sharedImageCache] clearDisk];
        
        //删除自己的
       // NSString *myCachePath = [NSHomeDirectory()stringByAppendingFormat:@"/Library/Caches/MyCaches"];
        
        
        //删除
      //  [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
        
    }
    
}
    





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
